<?php
/**
 * Created by PhpStorm.
 * User: alexey
 * Date: 5/26/14
 * Time: 8:17 PM
 */

function createArticleComment($newId, $content, $date, $time, $quote)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['content'] = $content;
  $paramArray['date'] = $date;
  $paramArray['time'] = $time;
  $paramArray['quote'] = $quote;
  $paramArray['news_id'] = $newId;
  return \controller\SQLManipulator::execute('insert-new-article-comment', $paramArray);
}

function saveNewArticleCommentImages($path, $respId)
{
  $paramArray = null;
  $paramArray['path'] = mysql_real_escape_string($path);
  $paramArray['resp_id'] = mysql_real_escape_string($respId);
  return \controller\SQLManipulator::execute('insert-new-article-comment-images', $paramArray);
}

function deleteSubscription($topicId)
{
  $paramArray = null;
  $paramArray['topic_id'] = $topicId;
  $paramArray['user_id'] = $_SESSION['id'];
  return \controller\SQLManipulator::execute('delete-subscription', $paramArray);
}

function deleteDialog($dialogId)
{
  $paramArray = null;
  $paramArray['dialog_id'] = $dialogId;
  $paramArray['sender_id'] = $_SESSION['id'];
  $paramArray['getter_id'] = $_SESSION['id'];
  return \controller\SQLManipulator::execute('delete-dialog', $paramArray);
}

function editTopicComment($commentId, $deleteQuote, $content)
{
  $paramArray = null;
  $paramArray['comment_id'] = $commentId;
  $paramArray['content'] = $content;
  if ($deleteQuote != 0) {
    return \controller\SQLManipulator::execute('update-topic-comment-delete-quote', $paramArray);
  } else {
    return \controller\SQLManipulator::execute('update-topic-comment', $paramArray);
  }
}

function createFlyComment($flyId, $content, $date, $time, $quote)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['fly_id'] = $flyId;
  $paramArray['content'] = $content;
  $paramArray['date'] = $date;
  $paramArray['time'] = $time;
  $paramArray['quote'] = $quote;
  return \controller\SQLManipulator::execute('insert-fly-comment', $paramArray);
}

function likeFlyComment($flyId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['likes_table'] = 'likes';
  $paramArray['content_id'] = $flyId;
  $paramArray['like_content'] = 'c';
  $paramArray['like_content_id'] = $flyId;
  $paramArray['table'] = 'flycomments';
  $paramArray['likes'] = 'likes';
  \controller\SQLManipulator::execute('update-comment-likes-table', $paramArray);
  \controller\SQLManipulator::execute('update-comment-likes', $paramArray);
}

function dislikeFlyComment($flyId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['likes_table'] = 'dislikes';
  $paramArray['content_id'] = $flyId;
  $paramArray['like_content'] = 'c';
  $paramArray['like_content_id'] = $flyId;
  $paramArray['table'] = 'flycomments';
  $paramArray['likes'] = 'dislikes';
  \controller\SQLManipulator::execute('update-comment-likes-table', $paramArray);
  \controller\SQLManipulator::execute('update-comment-likes', $paramArray);
}

function spamFly($flyId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['content_id'] = $flyId;
  $paramArray['table'] = 'flyspam';
  $paramArray['field'] = 'flycommentid';
  return \controller\SQLManipulator::execute('update-comment-spam', $paramArray);
}

function createFly($header, $legend, $date, $time)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['header'] = $header;
  $paramArray['legend'] = $legend;
  $paramArray['date'] = $date;
  $paramArray['time'] = $time;
  return \controller\SQLManipulator::execute('insert-new-fly', $paramArray);
}

function createFlyImage($path, $flyId)
{
  $paramArray = null;
  $paramArray['path'] = $path;
  $paramArray['fly_id'] = $flyId;
  return \controller\SQLManipulator::execute('insert-new-fly-image', $paramArray);
}

function updateFlyMainImage($imgId, $flyId)
{
  $paramArray = null;
  $paramArray['image_id'] = $imgId;
  $paramArray['fly_id'] = $flyId;
  return \controller\SQLManipulator::execute('update-new-fly-main-image', $paramArray);
}

function likeFly($flyId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['likes_table'] = 'likes';
  $paramArray['content_id'] = $flyId;
  $paramArray['like_content'] = 'f';
  $paramArray['like_content_id'] = $flyId;
  $paramArray['table'] = 'flies';
  $paramArray['likes'] = 'likes';
  \controller\SQLManipulator::execute('update-comment-likes-table', $paramArray);
  \controller\SQLManipulator::execute('update-comment-likes', $paramArray);
}

function dislikeFly($flyId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['likes_table'] = 'dislikes';
  $paramArray['content_id'] = $flyId;
  $paramArray['like_content'] = 'f';
  $paramArray['like_content_id'] = $flyId;
  $paramArray['table'] = 'flies';
  $paramArray['likes'] = 'dislikes';
  \controller\SQLManipulator::execute('update-comment-likes-table', $paramArray);
  \controller\SQLManipulator::execute('update-comment-likes', $paramArray);
}

function changeUserPass($newPassword, $userId)
{
  $paramArray = null;
  $paramArray['user_id'] = $userId;
  $paramArray['new_password'] = $newPassword;
  return \controller\SQLManipulator::execute('update-user-pass', $paramArray);
}

function createImageComment($imgId, $content, $date, $time, $quote)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['image_id'] = $imgId;
  $paramArray['content'] = $content;
  $paramArray['date'] = $date;
  $paramArray['time'] = $time;
  $paramArray['quote'] = $quote;
  return \controller\SQLManipulator::execute('insert-image-comment', $paramArray);
}

function likeImageComment($imgId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['likes_table'] = 'likes';
  $paramArray['content_id'] = $imgId;
  $paramArray['like_content'] = 'g';
  $paramArray['like_content_id'] = $imgId;
  $paramArray['table'] = 'galcomments';
  $paramArray['likes'] = 'likes';
  \controller\SQLManipulator::execute('update-comment-likes-table', $paramArray);
  \controller\SQLManipulator::execute('update-comment-likes', $paramArray);
}

function dislikeImageComment($imgId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['likes_table'] = 'dislikes';
  $paramArray['content_id'] = $imgId;
  $paramArray['like_content'] = 'c';
  $paramArray['like_content_id'] = $imgId;
  $paramArray['table'] = 'galcomments';
  $paramArray['likes'] = 'dislikes';
  \controller\SQLManipulator::execute('update-comment-likes-table', $paramArray);
  \controller\SQLManipulator::execute('update-comment-likes', $paramArray);
}

function spamImageComment($commentId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['content_id'] = $commentId;
  $paramArray['table'] = 'galspam';
  $paramArray['field'] = 'galid';
  return \controller\SQLManipulator::execute('update-comment-spam', $paramArray);
}

function likeImage($imgId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['likes_table'] = 'likes';
  $paramArray['content_id'] = $imgId;
  $paramArray['like_content'] = 'i';
  $paramArray['like_content_id'] = $imgId;
  $paramArray['table'] = 'gallery';
  $paramArray['likes'] = 'like';
  \controller\SQLManipulator::execute('update-comment-likes-table', $paramArray);
  \controller\SQLManipulator::execute('update-comment-likes', $paramArray);
}

function dislikeImage($imgId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['likes_table'] = 'dislikes';
  $paramArray['content_id'] = $imgId;
  $paramArray['like_content'] = 'i';
  $paramArray['like_content_id'] = $imgId;
  $paramArray['table'] = 'gallery';
  $paramArray['likes'] = 'dislike';
  \controller\SQLManipulator::execute('update-comment-likes-table', $paramArray);
  \controller\SQLManipulator::execute('update-comment-likes', $paramArray);
}

function likeArticleComment($respId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['likes_table'] = 'likes';
  $paramArray['content_id'] = $respId;
  $paramArray['like_content'] = 'r';
  $paramArray['like_content_id'] = $respId;
  $paramArray['table'] = 'responses';
  $paramArray['likes'] = 'likes';
  \controller\SQLManipulator::execute('update-comment-likes-table', $paramArray);
  \controller\SQLManipulator::execute('update-comment-likes', $paramArray);
}

function dislikeArticleComment($respId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['likes_table'] = 'dislikes';
  $paramArray['content_id'] = $respId;
  $paramArray['like_content'] = 'r';
  $paramArray['like_content_id'] = $respId;
  $paramArray['table'] = 'responses';
  $paramArray['likes'] = 'dislikes';
  \controller\SQLManipulator::execute('update-comment-likes-table', $paramArray);
  \controller\SQLManipulator::execute('update-comment-likes', $paramArray);
}

function markAllTopics()
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  \controller\SQLManipulator::execute('update-mark-all-topics', $paramArray);
}

function createNewMessage($content, $date, $time, $dialogId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['date'] = $date;
  $paramArray['content'] = $content;
  $paramArray['time'] = $time;
  $paramArray['dialog_id'] = $dialogId;
  return \controller\SQLManipulator::execute('insert-new-message', $paramArray);
}

function updateDialogAfterNewMessage($dialogId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['getter_id'] = $_SESSION['id'];
  $paramArray['dialog_id'] = $dialogId;
  \controller\SQLManipulator::execute('update-dialog-after-new-message', $paramArray);
}

function deleteDialogMessage($messageId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['message_id'] = $messageId;
  return \controller\SQLManipulator::execute('delete-dialog-message', $paramArray);
}

function createNewTopic($header, $content, $section)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['header'] = $header;
  $paramArray['content'] = $content;
  $paramArray['section'] = $section;
  $paramArray['lang'] = getLang($_SESSION['lang']);
  return \controller\SQLManipulator::execute('insert-new-topic', $paramArray);
}

function getLang($langString) {
  $startPos = stripos($langString, ".") + 1;
  return substr($langString, $startPos);
}

function checkNickOrEmail($login, $email)
{
  $paramArray = null;
  $paramArray['login'] = $login;
  $paramArray['email'] = $email;
  return \controller\SQLManipulator::execute('select-nick-or-email', $paramArray);
}

function createNewUser($firstName, $lastName, $nick, $dateRegistered, $email, $icq, $skype, $mobile, $address, $work, $methods, $password, $birth)
{
  $paramArray = null;
  $paramArray['nick'] = $nick;
  $paramArray['first_name'] = $firstName;
  $paramArray['last_name'] = $lastName;
  $paramArray['date'] = $dateRegistered;
  $paramArray['icq'] = $icq;
  $paramArray['skype'] = $skype;
  $paramArray['mobile'] = $mobile;
  $paramArray['address'] = $address;
  $paramArray['work'] = $work;
  $paramArray['methods'] = $methods;
  $paramArray['password'] = $password;
  $paramArray['birth'] = $birth;
  $paramArray['email'] = $email;
  return \controller\SQLManipulator::execute('create-new-user', $paramArray);
}

function insertNewUserIntoBaseGroup($userNick)
{
  $paramArray = null;
  $paramArray['nick'] = $userNick;
  return \controller\SQLManipulator::execute('insert-new-user-into-base-group', $paramArray);
}

function spamArticleComment($commentId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['content_id'] = $commentId;
  $paramArray['table'] = 'respspam';
  $paramArray['field'] = 'respid';
  return \controller\SQLManipulator::execute('update-comment-spam', $paramArray);
}

function deleteTopicComment($commentId, $topicId)
{
  $paramArray = null;
  $paramArray['comment_id'] = $commentId;
  $paramArray['topic_id'] = $topicId;
  return \controller\SQLManipulator::execute('delete-topic-comment', $paramArray);
}

function createTopicComment($topicId, $content, $date, $time, $quote)
{
  $paramArray = null;
  $paramArray['topic_id'] = $topicId;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['content'] = $content;
  $paramArray['date'] = $date;
  $paramArray['time'] = $time;
  $paramArray['quote'] = $quote;
  return \controller\SQLManipulator::execute('insert-new-topic-comment', $paramArray);
}

function updateUserTopicRead($topicId)
{
  $paramArray = null;
  $paramArray['topic_id'] = $topicId;
  $paramArray['user_id'] = $_SESSION['id'];
  return \controller\SQLManipulator::execute('update-user-topic-read', $paramArray);
}

function addNewTopicCommentImage($path, $date, $time)
{
  $paramArray = null;
  $paramArray['path'] = $path;
  $paramArray['date'] = $date;
  $paramArray['time'] = $time;
  return \controller\SQLManipulator::execute('insert-new-topic-comment-images', $paramArray);
}

function likeTopicComment($commentId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['likes_table'] = 'likes';
  $paramArray['content_id'] = $commentId;
  $paramArray['like_content'] = 't';
  $paramArray['like_content_id'] = $commentId;
  $paramArray['table'] = 'topicmessages';
  $paramArray['likes'] = 'likes';
  \controller\SQLManipulator::execute('update-comment-likes-table', $paramArray);
  \controller\SQLManipulator::execute('update-comment-likes', $paramArray);
}

function dislikeTopicComment($commentId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['likes_table'] = 'dislikes';
  $paramArray['content_id'] = $commentId;
  $paramArray['like_content'] = 't';
  $paramArray['like_content_id'] = $commentId;
  $paramArray['table'] = 'topicmessages';
  $paramArray['likes'] = 'dislikes';
  \controller\SQLManipulator::execute('update-comment-likes-table', $paramArray);
  \controller\SQLManipulator::execute('update-comment-likes', $paramArray);
}

function spamTopicComment($commentId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['content_id'] = $commentId;
  $paramArray['table'] = 'topicspam';
  $paramArray['field'] = 'topicmsgid';
  return \controller\SQLManipulator::execute('update-comment-spam', $paramArray);
}

function subscribeToTopic($topicId)
{
  $paramArray = null;
  $paramArray['user_id'] = $_SESSION['id'];
  $paramArray['topic_id'] = $topicId;
  return \controller\SQLManipulator::execute('insert-topic-subscription', $paramArray);
}

function getLastFliesId()
{
  return \controller\SQLManipulator::execute('select-last-flies-id', null);
}